<?php
namespace app\admin\controller;

use app\admin\controller\Base;
use think\Db;
use app\admin\model\Links as LinksModel;

class Links extends Base
{
    public function index()
    {
        return $this->fetch();
    }
	
	public function edit()
    {
		$id = input('id');
		$links = db('links')->where('id',$id)->find();
		
		if(request()->isPost()){
			$data = [
				'id' =>input('id'),
				'title' =>input('title'),
				'url' =>input('url'),
				'desc' =>input('description'),
			];
			
			
			// 验证
			$validate = \think\Loader::validate('Links');
			if(!$validate->check($data)){
			    $this->error($validate->getError());
				die;
			}
			
			if(db('links')->update($data)){
				$this->success('修改成功！','lst');
			}else{
				$this->error('修改失败！');
			}
			return;
		}
		
		
		$this->assign('links',$links);
        return $this->fetch();
    }
    
    public function add()
    {	
		if(request()->isPost()){
			// 接收数据
			$data=[
				'title'=>input('title'),
				'url'=>input('url'),
				'desc'=>input('description'),
			];
			
			// 验证
			$validate = \think\Loader::validate('Links');
			if(!$validate->check($data)){
			    $this->error($validate->getError());
				die;
			}
			
			// 判断结果
			if(Db::name('links')->insert($data)){
				return $this->success('添加链接成功','lst');
			}else{
				return $this->error('添加链接失败');
			}
			return;
		}
        return $this->fetch('add');
    }
    
    public function lst()
    {	
		$list = LinksModel::paginate(5);
		$this->assign('list',$list);
        return $this->fetch('lst');
    }
	
	public function del(){
		$id = input('id');
		if(db('links')->delete(input('id'))){
			$this->success('删除链接成功','lst');
		}else{
			$this->error('删除链接失败');
		}
	}
	
	
}
